﻿/*
* Copyright (c) 2007-2010 SlimDX Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using SlimDX;

namespace Editor.Mesh
{
    /// <summary>
    /// A container for mesh data.
    /// </summary>
    public class MeshData
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct VertexPositionNormalUV
        {
            public Vector3 position;
            public Vector3 normal;
            public Vector2 uv;

            public VertexPositionNormalUV(Vector3 _position, Vector3 _normal, Vector2 _uv)
            {
                position = _position;
                normal = _normal;
                uv = _uv;
            }
        };

        #region Members
        private String m_MeshFileName;
        private List<Vector3> positions = new List<Vector3>();
        private List<Vector3> normals = new List<Vector3>();
        private List<Vector2> textureCoordinates = new List<Vector2>();
        private List<int> indices = new List<int>();

        private SlimDX.Direct3D9.VertexBuffer m_VertexBuffer;
        private SlimDX.Direct3D9.IndexBuffer m_IndexBuffer;
        #endregion

        #region Properties
        public String MeshFileName
        {
            get { return m_MeshFileName; }
        }

        public List<Vector3> Positions
        {
            get { return positions; }
        }

        public List<Vector3> Normals
        {
            get { return normals; }
        }

        public List<Vector2> TextureCoordinates
        {
            get { return textureCoordinates; }
        }

        public List<int> Indices
        {
            get { return indices; }
        }

        public SlimDX.Direct3D9.VertexBuffer VertexBuffer
        {
            get { return m_VertexBuffer; }
        }

        public SlimDX.Direct3D9.IndexBuffer IndexBuffer
        {
            get { return m_IndexBuffer; }
        }
        #endregion

        public MeshData()
        {
        }

        public MeshData(String fileName)
        {
            m_MeshFileName = fileName;
        }

        public void CreateRenderingBuffers(SlimDX.Direct3D9.Device device)
        {
            if (Indices.Count > 0)
            {
                m_IndexBuffer = new SlimDX.Direct3D9.IndexBuffer(device,
                                                  Indices.Count * Marshal.SizeOf(typeof(UInt16)),
                                                  SlimDX.Direct3D9.Usage.WriteOnly,
                                                  SlimDX.Direct3D9.Pool.Managed,
                                                  true);

                DataStream indexStream = m_IndexBuffer.Lock(0, 0, SlimDX.Direct3D9.LockFlags.None);
                for (int i = 0; i < Indices.Count; i++)
                {
                    UInt16 index = (UInt16)Indices[i];
                    indexStream.Write(index);
                }
                m_IndexBuffer.Unlock();
            }

            m_VertexBuffer = new SlimDX.Direct3D9.VertexBuffer(device,
                                              Positions.Count * Marshal.SizeOf(typeof(VertexPositionNormalUV)),
                                              SlimDX.Direct3D9.Usage.WriteOnly,
                                              SlimDX.Direct3D9.VertexFormat.None,
                                              SlimDX.Direct3D9.Pool.Managed);

            DataStream vertexStream = m_VertexBuffer.Lock(0, 0, SlimDX.Direct3D9.LockFlags.None);
            for (int i = 0; i < Positions.Count; i++)
            {
                Vector3 position = Positions[i];
                Vector3 normal = Normals[i];
                Vector2 uv = TextureCoordinates[i];
                VertexPositionNormalUV vertex = new VertexPositionNormalUV(position, normal, uv);
                vertexStream.Write(vertex);
            }
            m_VertexBuffer.Unlock();
        }
    }
}
